package com.ehotting.mqbd.core.mq.consumer.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.ehotting.eaf.core.utils.SpringUtils;
import com.ehotting.mqbd.api.ConvertClass;
import com.ehotting.mqbd.api.bean.MQDataAble;
import com.ehotting.mqbd.api.bean.MQHttpData;
import com.ehotting.mqbd.core.logger.MqMsgLogger;
import com.ehotting.mqbd.core.manager.ModuleManager;
import com.ehotting.mqbd.core.service.storage.RedisMqStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;

public abstract  class ConsumerActionAbstract implements ConsumerActionAble {

    protected final  Logger logger = LoggerFactory.getLogger(this.getClass());

    protected MqMsgLogger mqMsgLogger;

    protected ModuleManager moduleManager;



    public MQDataAble convert( String msg,String msgId){
        logger.info("Start convert.");
        mqMsgLogger.info(msg);
        MQDataAble data = null;
        ConvertClass baseData = JSON.parseObject(msg, ConvertClass.class, Feature.OrderedField);
        String clazz = baseData.getClazz();
        logger.info("baseData.getClazz():{}",clazz);
        try {
            data = (MQDataAble) JSON.parseObject(msg,  (Type) Class.forName(clazz));
            data.setMsgId(msgId);
        } catch (ClassNotFoundException e) {
            logger.error(e.getMessage());
            data = JSON.parseObject(msg, MQHttpData.class);
        }


        RedisMqStorage redisStorage = SpringUtils.getBean(RedisMqStorage.class);
        data = redisStorage.handleGet(data);

        logger.info("Finish convert.");
        return data;
    }




}